<?php

namespace FacebookAction;

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );

//use Facebook\FacebookSession;
//use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
//use Facebook\FacebookResponse;
//use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
//use Facebook\FacebookAuthorizationException;
//use Facebook\GraphObject;
//use Facebook\GraphUser;
//use Facebook\GraphSessionInfo;

class FacebookPhoto {

    //----------------- publish photo len trang admin --------
    public function publishPhotoToAdmin($session, $photo_path, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/me/photos', array(
                'source' => new CURLFile($photo_path, 'image/jpg'),
                'message' => $message
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //------------ publish photo to Fanpge --------------------
    public function publishPhotoToFanpage($session, $page_id, $access_token, $photo_path, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/' . $page_id . '/photos', array(
                'access_token' => $access_token,
                'source' => new CURLFile($photo_path, 'image/jpg'),
                'message' => $message
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //----------- publish photo to specific album ---------------------------
    public function publishPhotoToAlbum($session, $album_id, $access_token, $photo_path, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/' . $album_id . '/photos', array(
                'access_token' => $access_token,
                'source' => $photo_path,
                'message' => $message
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //----------Lấy tất cả photo trong một album
    public function getAllPhotoFromAlbum($session, $album_id) {
        try {
            $request = new FacebookRequest(
                    $session, 'GET', '/' . $album_id . '/photos'
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
//            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //--------- Lấy về  photo có số lượng like cao nhất ------------------------
    public function getPhotoLikeHightestInAlbum($session, $album_id){
        try {
            $request = new FacebookRequest(
                    $session, 'GET', '/' . $album_id . '/photos'
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }
}
